package util;

/**
 * 
 * 读取配置文件的工具类 
 * 
 * 
 * */
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.support.PropertiesLoaderSupport;

public class Configuration extends PropertiesLoaderSupport {

	private static final Logger log = LoggerFactory
			.getLogger(Configuration.class);

	private static Properties properties = null;

	static {

		log.info("配置文件初始化开始");
		FileInputStream fileis;
		try {
			Map<String, String> m = System.getenv();

			// 会自动去获取该路径下配置文件
			String path = "D:/eclipse32Pro/myFirstMavenPro/src/main/resources/config.properties";
			// String path = m.get("JBOSS_HOME")
			// + "/server/default/conf/zqswnewswebsite_config.properties";
			// String path =
			// m.get("TOMCAT_HOME")+"/server/default/conf/zqswnewswebsite_config.properties";

			path = path.replace("//", File.separator);

			log.info("配置文件初始化路径", path);

			fileis = new FileInputStream(path);

			properties = new Properties();

			properties.load(fileis);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("文件初始化异常.", e);
		}

	}

	/**
	 * 读取配置文件信息
	 *
	 * @param name
	 *            读取节点名
	 * @param fileName
	 *            文件名
	 * @return 读取的节点值
	 */
	public static String readConfigString(String name, String fileName) {
		String result = "";
		try {
			ResourceBundle rb = ResourceBundle.getBundle(fileName);
			result = rb.getString(name);
		} catch (Exception e) {
			log.error("从" + fileName + "读取" + name + "出错:" + e.getMessage());
		}
		return result;
	}

	/**
	 * 读取配置文件信息
	 * 
	 * @param key
	 *            读取节点名
	 * @return 读取的节点值
	 */
	public static String readConfigString(String key) {
		String result = "";
		if (properties != null) {
			return properties.getProperty(key);
		}
		return result;
	}

	public static void main(String args[]) {
		System.out.println(readConfigString("redis.ip"));
		System.out.println("222");
		ResourceBundle rb = ResourceBundle.getBundle("config");
		String result = rb.getString("redis.ip");
		System.out.println(result);
	}

}
